package com.ncloud.msg.dubboservice.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.msg.dubboservice.MailDubboService;
import com.ncloud.msg.entity.Email;
import com.ncloud.msg.entity.EmailConfig;
import com.ncloud.msg.mapper.EmailMapper;
import com.ncloud.param.service.SequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 * Created by shiqiang on 2017/5/15.
 */
@Service(delay=-1,retries=-1,timeout = 2000)
public class MailDubboServiceImpl implements MailDubboService{
    @Autowired
    private EmailMapper emailMapper;
    @Autowired
    private EmailConfig emailConfig;
    @Reference
    private SequenceService sequenceService;
    @Override
    public JSONObject insertEmail(JSONObject email) {
        JSONObject result = new JSONObject();
        result.put("msgCode","1");
        result.put("msgDesc","成功");
        try {
            Email mail = validated(email);
            result.put("msgId",mail.getMsgId());
            emailMapper.insertEmail(mail);
        } catch (Exception e) {
            result.put("msgCode","9");
            result.put("msgDesc","发送失败");
            e.printStackTrace();
            throw new UserException(e.getMessage());
        }
        return result;
    }


    private Email validated(JSONObject param){
        if(StringUtils.isEmpty(param)){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"发送邮件参数信息为空，请检查！");
        }
        Email email = JSON.parseObject(param.toJSONString(),Email.class);
        if(StringUtils.isEmpty(email.getSysCode())){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"系统编码未设置：sysCode");
        }
        if(StringUtils.isEmpty(email.getSubject())){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"邮件主题未设置：subject");
        }
        if(StringUtils.isEmpty(email.getMsgFrom())){
           //email.setMsgFrom(emailConfig.getEmailFrom());
            email.setMsgFrom("open-ncloudadmin@btte.net");
        }
        if(StringUtils.isEmpty(email.getMsgTo())){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"接收邮件人未设置：msgTo");
        }
      /*  if(StringUtils.isEmpty(email.getContent())){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"邮件内容未设置：content");
        }*/

        //
        if(param.containsKey("content")){
            if(StringUtils.isEmpty(email.getMsgType())){
                email.setMsgType("0");
            }else if("1".equals(email.getMsgType())){
                if(StringUtils.isEmpty(email.getTemplateName())){
                    throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"请检查模板名称设置：templateName");
                }
                Object obj = param.get("content");
                if(obj instanceof JSONObject){
                    email.setContent(((JSONObject) obj).toJSONString());
                }else if (obj instanceof String){
                    email.setContent(JSON.toJSONString(obj));
                }else{
                    throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"请检查模板参数设置，必须为json对象：content");
                }
            }
        }else{
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"邮件内容未设置：content");
        }

        if(email.getMsgId() == null){
            Long id = sequenceService.getSequence("seq_msg_id");
            email.setMsgId(id);
        }
        //设置默认初始状态
        if(StringUtils.isEmpty(email.getMsgCode())){
            email.setMsgCode("0");
        }
        email.setMsgResult("入表成功");
        return email;
    }
}
